package com.example.service;


import com.example.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

@Service
public class SendEmailService {
    @Autowired
    private JavaMailSenderImpl mailSender;
    @Value("${spring.mail.username}")
    private String from;
    public ResultEntity<String> sendSimpleEmail(String to, String subject){
        int builder=0;
        for (int i = 0; i < 4; i++) {
            int random = (int) (Math.random() * 10);
            builder=(builder+random)*10;
        }
        String text =builder/10+"";
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject( subject);
        message.setText(text);
        try {
            // 发送邮件
            mailSender.send(message);
            return   ResultEntity.successWithData(text);//("纯文本邮件发送成功");
        } catch (MailException e) {
            System.out.println("纯文本邮件发送失败 " + e.getMessage());
            //e.printStackTrace();
            return   ResultEntity.failed("纯文本邮件发送失败");
        }
    }

}
